package com.jdd.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.dao.entity.TArea;
import com.jdd.dao.entity.TBuilding;
import com.jdd.dao.mapper.TAreaMapper;
import com.jdd.dao.mapper.TBuildingMapper;
import com.jdd.dao.service.ITBuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.dao.vo.BuildingSerachVo;
import com.jdd.dao.vo.BuildingVo;
import com.jdd.dao.vo.SpaceVo;
import com.jdd.modules.userstaff.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 楼栋单元表 服务实现类
 * </p>
 *
 * @author ls
 * @since 2022-09-19
 */
@Service
@Slf4j
public class TBuildingServiceImpl extends ServiceImpl<TBuildingMapper, TBuilding> implements ITBuildingService {

    @Resource
    private TBuildingMapper tBuildingMapper;
    @Resource
    private TAreaMapper tAreaMapper;
    @Resource
    private UserMapper tUserMapper;
    @Override
    public IPage<BuildingVo> getBuildingInfoPage(Page<BuildingVo> page, BuildingSerachVo buildingSerachVo, List<String> projectCodes) {
        IPage<BuildingVo> buildingVoIPage = tBuildingMapper.selectBuildingPage(page,buildingSerachVo,projectCodes);
        List<BuildingVo> records = buildingVoIPage.getRecords();
        for (BuildingVo record : records) {
            int peopleNum = tUserMapper.selectByHouseId(record.getId());
            record.setPeopleNum(peopleNum);
        }

        return buildingVoIPage;
    }

    @Override
    public Result<?> addNew(SpaceVo space) {
        log.info("-----新增楼栋-----");
        Result<?> result = new Result<>();
        LoginUser loginUser = LoginUserUtils.getLoginUser();
        List<String> buildings = new ArrayList<>();
        Integer startNumber = space.getStartNumber();
        Integer placeNumbers = space.getNumbers();
        Integer limitNumber = space.getLimitNumber();
        String identCode = space.getIdentCode();
        LambdaQueryWrapper<TBuilding> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TBuilding::getAreaId,space.getId());
        int count = this.count(queryWrapper1);
        if (count+placeNumbers>100){
            return result.error500("一个区域创建的楼栋数不能超过100");
        }
        for (int i = 0; i < placeNumbers; i++) {
            buildings.add(getBuildingName(startNumber,i,identCode,limitNumber));
        }
        LambdaQueryWrapper<TBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TBuilding::getAreaId,space.getId());
        queryWrapper.in(TBuilding::getBuildingName,buildings);
        List<TBuilding> list=this.list(queryWrapper);
        if (list.size()>0){
            return result.error500("该区间存在相同楼栋");
        }

        TBuilding tBuilding;
        List<TBuilding> spaceList = new LinkedList<>();
        TArea  tArea = tAreaMapper.selectById(space.getId());
        String parentCode = tArea.getParentCode()+"A"+tArea.getCode();
        String sysProjectCode = tArea.getSysProjectCode();
        for (String buildingName : buildings){
            tBuilding = new TBuilding();
            tBuilding.setId(SnowFlakeUtil.getId());
            tBuilding.setAreaId(space.getId());
            tBuilding.setProjectId(tArea.getProjectId());
            tBuilding.setBuildingName(buildingName);
            tBuilding.setCreateUser(loginUser.getUsername());
            tBuilding.setSysProjectCode(sysProjectCode);
            tBuilding.setParentCode(parentCode);
            spaceList.add(tBuilding);
        }
        this.saveBatch(spaceList);
        log.info("-----新增楼栋成功------{}",spaceList);
        return Result.ok("新增成功！");
    }


    private String getBuildingName(Integer startNumber, Integer placeNumbers, String identCode,Integer limitNumber) {
        if (identCode==null){
            identCode = "";
        }
        int num = startNumber + placeNumbers;
        String number=num+"";
        while (number.length()<limitNumber){
            number ='0'+number;
        }
        number = identCode+number;
        return number;
    }
}
